using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Threading;

namespace Oracle.ManagedDataAccess.Client
{
	internal static class OracleStringResourceManager
	{
		private const string RESOURCE_NAME_PREFIX = "Oracle.ManagedDataAccess.src.Client.Resources.Exception";

		private const string RESOURCE_NAME_PREFIX_WITH_DOT = "Oracle.ManagedDataAccess.src.Client.Resources.Exception.";

		private const string RESOURCE_NAME_SUFFIX = "resources";

		private const string RESOURCE_NAME_SUFFIX_WITH_DOT = ".resources";

		private const char DOT = '.';

		private static readonly string DFEAULT_RESOURCE_NAME;

		private static string DEFAULT_MESSAGE_NUMBER;

		private static Dictionary<string, ResourceSet> m_CultureToResourceStringMap;

		static OracleStringResourceManager()
		{
			DFEAULT_RESOURCE_NAME = string.Empty;
			DEFAULT_MESSAGE_NUMBER = Convert.ToString(-12);
			m_CultureToResourceStringMap = null;
			m_CultureToResourceStringMap = (from resourceName in Assembly.GetExecutingAssembly().GetManifestResourceNames()
				where resourceName.StartsWith("Oracle.ManagedDataAccess.src.Client.Resources.Exception") && resourceName.EndsWith(".resources")
				select resourceName).ToDictionary((Func<string, string>)((string resourceName) => resourceName.Substring("Oracle.ManagedDataAccess.src.Client.Resources.Exception".Length, resourceName.Length - ("Oracle.ManagedDataAccess.src.Client.Resources.Exception".Length + "resources".Length)).Trim('.')), (Func<string, ResourceSet>)((string resourceName) => null));
		}

		internal static string GetString(string key, CultureInfo culture)
		{
			try
			{
				foreach (string item in from cultureName in culture.NextMatchingNonInvariantCulture()
					where m_CultureToResourceStringMap.ContainsKey(cultureName)
					select cultureName)
				{
					string resourceStringForCultureName = GetResourceStringForCultureName(key, item);
					if (resourceStringForCultureName != null)
					{
						return resourceStringForCultureName;
					}
				}
				return GetResourceStringForCultureName(key, DFEAULT_RESOURCE_NAME);
			}
			catch
			{
				return null;
			}
		}

		private static string GetResourceStringForCultureName(string key, string cultureName)
		{
			if (!m_CultureToResourceStringMap.ContainsKey(cultureName))
			{
				return null;
			}
			if (m_CultureToResourceStringMap[cultureName] == null)
			{
				ExtractEmbeddedResourceStringsForCultureName(cultureName);
			}
			return m_CultureToResourceStringMap[cultureName]?.GetString(key);
		}

		private static void ExtractEmbeddedResourceStringsForCultureName(string name)
		{
			if (m_CultureToResourceStringMap.ContainsKey(name) && m_CultureToResourceStringMap[name] == null)
			{
				string name2 = "Oracle.ManagedDataAccess.src.Client.Resources.Exception" + ((string.IsNullOrEmpty(name) || name.Trim().Length == 0) ? ".resources" : (name.Trim() + ".resources"));
				Stream manifestResourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(name2);
				if (manifestResourceStream != null)
				{
					m_CultureToResourceStringMap[name] = new ResourceSet(manifestResourceStream);
				}
			}
		}

		private static IEnumerable<string> NextMatchingNonInvariantCulture(this CultureInfo culture)
		{
			CultureInfo ci = culture;
			while (ci != null && ci != CultureInfo.InvariantCulture)
			{
				yield return ci.Name;
				ci = ci.Parent;
			}
		}

		internal static string GetErrorMesg(int errorcode, params string[] args)
		{
			string text = string.Empty;
			string @string = GetString(Convert.ToString(errorcode), Thread.CurrentThread.CurrentCulture);
			if (@string != null)
			{
				text = string.Format(@string, args);
				if (errorcode > 0)
				{
					if (text.StartsWith("Ora-12541", StringComparison.InvariantCulture))
					{
						text = text.Replace("Ora-12541", "ORA-12541");
					}
					if (!text.StartsWith("ORA-", StringComparison.InvariantCultureIgnoreCase))
					{
						text = string.Format("ORA-{0}: {1}", errorcode.ToString("D5"), text);
					}
				}
			}
			else if ((@string = GetString(DEFAULT_MESSAGE_NUMBER, Thread.CurrentThread.CurrentCulture)) != null)
			{
				text = string.Format(@string, errorcode);
			}
			return text;
		}

		internal static string GetErrorMesgWithErrCode(int errorcode, params string[] args)
		{
			string errorMesg = GetErrorMesg(errorcode, args);
			if (errorcode > 0)
			{
				return errorMesg;
			}
			return string.Format("ORA-{0}: {1}", errorcode.ToString("D5"), errorMesg);
		}
	}
}
